﻿/*  ------------------------------------------------------------------------
	 NQ Common Services Library
	 
	 Homepage: http://www.awzhome.de/
	 ------------------------------------------------------------------------
	 
	 The contents of this file are subject to the Mozilla Public License
	 Version 1.1 (the "License"); you may not use this file except in
	 compliance with the License. You may obtain a copy of the License at
	 http://www.mozilla.org/MPL/
	 
	 Software distributed under the License is distributed on an "AS IS"
	 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	 License for the specific language governing rights and limitations
	 under the License.
	
	 The Original Code is code of NQ Common Services Library.

	 The Initial Developer of the Original Code is Andreas Weizel.
	 Portions created by the Initial Developer are
	 Copyright (C) 2011 Andreas Weizel. All Rights Reserved.
	 
	 Contributor(s): (none)
	 
	 Alternatively, the contents of this file may be used under the terms
	 of the GNU General Public License Version 2.0 or later (the "GPL"),
	 or the GNU Lesser General Public License Version 2.1 or later (the
	 "LGPL"), in which case the provisions of the GPL or the LGPL are
	 applicable instead of those above. If you wish to allow use of your
	 version of this file only under the terms of the GPL or the LGPL and
	 not to allow others to use your version of this file under the MPL,
	 indicate your decision by deleting the provisions above and replace
	 them with the notice and other provisions required by the GPL or the
	 LGPL. If you do not delete the provisions above, a recipient may use
	 your version of this file under the terms of any of the MPL, the GPL
	 or the LGPL.
	 ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AWZhome.NQ.Core.Fluent;
using AWZhome.NQ.Core;

namespace AWZhome.NQ.CommonServices.Fluent
{
	/// <summary>
	/// Class for working with the <see cref="AWZhome.NQ.CommonServices.Services.IConfigService"/>
	/// using a fluent interface.
	/// </summary>
	public static class Configuration
	{
		/// <summary>
		/// Loads the configuration from storage.
		/// </summary>
		public static void Load()
		{
			IConfigManager configService = ServiceCore
				.Get<Services.IConfigService>()
				.To;
			configService.LoadConfiguration();
		}

		/// <summary>
		/// Saves the configuration to storage.
		/// </summary>
		public static void Save()
		{
			try
			{
				IConfigManager configService = ServiceCore
					.Get<Services.IConfigService>()
					.To;
				configService.SaveConfiguration();
			}
			catch (NQServiceLoadingException)
			{
				// There seems to be no ConfigService, ignore this
			}
		}

		/// <summary>
		/// Returns a wrapper for performing operations on single configuration values.
		/// </summary>
		/// <param name="name">Name of the value to work on.</param>
		/// <returns>Instance of the value wrapper object.</returns>
		public static ConfigurationValueWrapper Value(string name)
		{
			return new ConfigurationValueWrapper(name);
		}

		/// <summary>
		/// Returns a wrapper for performing operations on configuration lists.
		/// </summary>
		/// <param name="name">Name of the list to work on.</param>
		/// <returns>Instance of the list wrapper object.</returns>
		public static ConfigurationListWrapper List(string name)
		{
			return new ConfigurationListWrapper(name);
		}

		/// <summary>
		/// Returns a wrapper for performing operations on configuration groups.
		/// </summary>
		/// <param name="name">Name of the group to work on.</param>
		/// <returns>Instance of the group wrapper object.</returns>
		public static ConfigurationGroupWrapper Group(string name)
		{
			return new ConfigurationGroupWrapper(name);
		}

		/// <summary>
		/// Returns list of all configuration categories.
		/// </summary>
		/// <returns>List of category names as string array.</returns>
		public static string[] Categories
		{
			get
			{
				try
				{
					IConfigManager configService = ServiceCore
						.Get<Services.IConfigService>()
						.To;
					return configService.GetCategoryList();
				}
				catch (NQServiceLoadingException)
				{
					return new string[] { };
				}
			}
		}

		/// <summary>
		/// Returns list of all groups in a category.
		/// </summary>
		/// <returns>List of group names as string array.</returns>
		public static string[] GroupsOf(string categoryName)
		{
			try
			{
				IConfigManager configService = ServiceCore
					.Get<Services.IConfigService>()
					.To;
				return configService.GetGroupList(categoryName);
			}
			catch (NQServiceLoadingException)
			{
				return new string[] { };
			}
		}
	}
}
